#include"stdio.h"
#include"memory.h"
#include"stdlib.h"
#include"stddef.h"
#include"unistd.h"
#include"fcntl.h"
#include"sys/types.h"
#include"string.h"
#include"time.h"

#include"httpresponse.h"

char *gmStrUpr(char *str)
{
	char *ptr;
	ptr = str;
	while (*ptr)
	{
		if (*ptr >= 0x61 && *ptr <= 0x7a)
			*ptr = (*ptr) - 0x20;
		ptr++;
	}
	return str;

}

HttpResponse *HttpResponseNew(void)//int responsecode,int contentlength,int timeout)
{
	HttpResponse *res;
	res = (HttpResponse*)malloc(sizeof(HttpResponse));
	res->HeadCmd[0] = 0;
	res->Cache_Control[0] = 0;
	res->Connection[0] = 0;
	res->Date[0] = 0;
	res->Pragma[0] = 0;
	res->Trailer[0] = 0;
	res->Transfer_Encoding[0] = 0;
	res->Upgrade[0] = 0;
	res->Via[0] = 0;
	res->Warning[0] = 0;
	res->Accept_Ranges[0] = 0;
	res->Age[0] = 0;
	res->ETag[0] = 0;
	res->Location[0] = 0;
	res->Proxy_Authenticate[0] = 0;
	res->Retry_After[0] = 0;
	res->Server[0] = 0;
	res->Vary[0] = 0;
	res->WWW_Authenticate[0] = 0;
	res->Allow[0] = 0;
	res->Content_Encoding[0] = 0;
	res->Content_Language[0] = 0;
	res->Content_Length[0] = 0;
	res->Content_Location[0] = 0;
	res->Content_MD5[0] = 0;
	res->Content_Range[0] = 0;
	res->Content_Type[0] = 0;
	res->Expires[0] = 0;
	res->Last_Modified[0] = 0;
	res->Keep_Alive[0] = 0;
	return res;
}
int HttpResponseSetStatusCode(HttpResponse *res,int code, char *discrp)
{
	snprintf(res->HeadCmd,63, "%s %d %s\r\n", HTTPVersion, code, discrp);
	return 0;
}

int HttpResponseSetConnection(HttpResponse *res, char *connstr)
{
	snprintf(res->Connection,60, "Connection: %s\r\n",connstr);
	return 0;
}
int HttpResponseSetDate(HttpResponse *res)
{
	//  getdate
	time_t t;
	time(&t);
	snprintf(res->Date,63,"Date: %s",ctime(&t));
	return 0;
}
int HttpResponseSetETag(HttpResponse *res, char * etag)
{
	return 0;
}
int HttpResponseSetServer(HttpResponse *res, char *server)
{
	snprintf(res->Server,63, "Server: %s\r\n", server);

	return 0;
}
int HttpResponseSetContentLength(HttpResponse *res, int cl)
{
	snprintf(res->Content_Length,63, "Content-Length:%d\r\n", cl);
	return 0;
}
int HttpResponseSetContentType(HttpResponse *res, char *ct)
{
	snprintf(res->Content_Type,63, "Content-Type:%s\r\n", ct);
	return 0;
}
int HttpResponseSetWWWAuthen(HttpResponse *res, char *aumode, char *realm)
{
	return 0;
}
int HttpResponseSetLastModified(HttpResponse *res)
{
	time_t t;
	t = time(NULL);
	snprintf(res->Last_Modified,63, "Last-Modified: %s\r\n", asctime(localtime(&t)) );
	return 0;
}
int HttpResponseSetKeepAlive(HttpResponse *res, int timeout)
{
	snprintf(res->Keep_Alive, 63,"Keep-Alive: timeout=%d max=100\r\n", timeout);
	return 0;
}

int HttpResponseSetCacheControl(HttpResponse *res, char *value)
{
	snprintf(res->Cache_Control,63, "Cache-Control: %s\r\n", value);
	return 0;
}
int HttpResponseSetPragma(HttpResponse *res, char *value)
{
	snprintf(res->Pragma, 63, "Pragma: %s\r\n", value);
	return 0;
}
int HttpResponseSetExpires(HttpResponse *res, char *value)
{
	snprintf(res->Expires, 63, "Expires: %s\r\n", value);
	return 0;
}

/*
{
AnsiString str;
strcpy(HeadCmd,HTTPVersion);
switch(responsecode)
{
case 200:
strcat(HeadCmd,"200 OK\r\n");
break;
default:
strcat(HeadCmd,"401 error\r\n");
break;
}
str = DateTimeToStr(Now());
sprintf(Date,"Date: %s\r\n",str.c_str());
sprintf(Server,"Server: %s\r\n",SERVER);
sprintf(Last_Modified,"Last-Modified: %s\r\n",str.c_str());
sprintf(ETag,"ETag: \"%s\"\r\n","123-234-56789abc");
sprintf(Accept_Ranges,"Accept-Ranges: bytes\r\n") ;
sprintf(Content_Length,"Content-Length:%d\r\n",contentlength);
sprintf(Keep_Alive,"Keep-Alive:timeout=%d max=100\r\n",timeout);
sprintf(Connection,"Connection: Keep_Alive\r\n");
sprintf(Content_Type,"Content-Type:text/html\r\n");

}
*/
int HttpResponseDelete(HttpResponse *res)
{
	free(res);
	return 0;
}
int HttpResponseGetResponseHead(HttpResponse *res, char *headstring)
{
	if (res->HeadCmd[0] != 0)
		strcpy(headstring, res->HeadCmd);
	if (res->Cache_Control[0] != 0)
		strcat(headstring, res->Cache_Control);
	if (res->Connection[0] != 0)
		strcat(headstring, res->Connection);
	if (res->Date[0] != 0)
		strcat(headstring, res->Date);
	if (res->Pragma[0] != 0)
		strcat(headstring, res->Pragma);
	if (res->Trailer[0] != 0)
		strcat(headstring, res->Trailer);
	if (res->Transfer_Encoding[0] != 0)
		strcat(headstring, res->Transfer_Encoding);
	if (res->Upgrade[0] != 0)
		strcat(headstring, res->Upgrade);
	if (res->Via[0] != 0)
		strcat(headstring, res->Via);
	if (res->Warning[0] != 0)
		strcat(headstring, res->Warning);
	if (res->Accept_Ranges[0] != 0)
		strcat(headstring, res->Accept_Ranges);
	if (res->Age[0] != 0)
		strcat(headstring, res->Age);
	if (res->ETag[0] != 0)
		strcat(headstring, res->ETag);
	if (res->Location[0] != 0)
		strcat(headstring, res->Location);
	if (res->Proxy_Authenticate[0] != 0)
		strcat(headstring, res->Proxy_Authenticate);
	if (res->Retry_After[0] != 0)
		strcat(headstring, res->Retry_After);
	if (res->Server[0] != 0)
		strcat(headstring, res->Server);
	if (res->Vary[0] != 0)
		strcat(headstring, res->Vary);
	if (res->WWW_Authenticate[0] != 0)
		strcat(headstring, res->WWW_Authenticate);
	if (res->Allow[0] != 0)
		strcat(headstring, res->Allow);
	if (res->Content_Encoding[0] != 0)
		strcat(headstring, res->Content_Encoding);
	if (res->Content_Language[0] != 0)
		strcat(headstring, res->Content_Language);
	if (res->Content_Length[0] != 0)
		strcat(headstring, res->Content_Length);
	if (res->Content_Location[0] != 0)
		strcat(headstring, res->Content_Location);
	if (res->Content_MD5[0] != 0)
		strcat(headstring, res->Content_MD5);
	if (res->Content_Range[0] != 0)
		strcat(headstring, res->Content_Range);
	if (res->Content_Type[0] != 0)
		strcat(headstring, res->Content_Type);
	if (res->Expires[0] != 0)
		strcat(headstring, res->Expires);
	if (res->Last_Modified[0] != 0)
		strcat(headstring, res->Last_Modified);
	if (res->Keep_Alive[0] != 0)
		strcat(headstring, res->Keep_Alive);

	strcat(headstring, "\r\n");
	return 0;
}

extern char buildEDITION[];
// image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*
int HttpResponseSetResponseHead(HttpResponse *res,int code, char *descript,int cl, char *filepath)
{
	int ct;
	if (code >= 200 && code < 500)
	{
		HttpResponseSetStatusCode(res, code, descript);
		HttpResponseSetServer(res, "zuchongzhi");
		HttpResponseSetConnection(res, "close");
		HttpResponseSetCacheControl(res, "no-cache");
		HttpResponseSetPragma(res, "no-cache");
		HttpResponseSetDate(res);
		HttpResponseSetContentLength(res, cl);
		ct = HttpResponseGetContentType(filepath);
		switch (ct)
		{
			case CTYPE_SWF:
				HttpResponseSetContentType(res, "application/x-shockwave-flash");
				break;
			case CTYPE_GIF:
				HttpResponseSetContentType(res, "image/gif");
				break;
			case CTYPE_JPEG:
				HttpResponseSetContentType(res, "image/jpeg");
				break;
			case CTYPE_PJPEG:
				HttpResponseSetContentType(res, "image/pjpeg");
				break;
			case CTYPE_BMP:
				HttpResponseSetContentType(res, "image/x-xbitmap");
				break;
			case CTYPE_PLAIN:
				HttpResponseSetContentType(res, "text/plain");//;charset=gb2312");
				break;
			case CTYPE_HTML:
				HttpResponseSetContentType(res, "text/html");//;charset=gb2312");
				break;
			case CTYPE_JS:
				HttpResponseSetContentType(res, "application/javascript");//;charset=gb2312");
				break;
			case CTYPE_CSS:
				HttpResponseSetContentType(res, "text/css");//;charset=gb2312");
				break;
			case CTYPE_XML:
				HttpResponseSetContentType(res, "application/xml");//;charset=gb2312");
				break;
			case CTYPE_SOAPXML:
				HttpResponseSetContentType(res, "application/soap+xml");//;charset=gb2312");
				break;
			case CTYPE_MP4:
				HttpResponseSetContentType(res, "video/mp4");
				break;
			case CTYPE_WMV:
				HttpResponseSetContentType(res, "video/wmv");
				break;
			default:
				HttpResponseSetContentType(res, "application/octec-stream");
		}
	}
	return 0;
}
int HttpResponseGetContentType(char *filepath)
{
	//   strupr stricmp
	char path[256];
	char *ptr;
	if(filepath==NULL)
	    return CTYPE_OCTEC;
	strcpy(path, filepath);
	ptr = gmStrUpr(path);
	if (strstr(ptr, ".JPG") || strstr(ptr, ".JPEG"))
		return CTYPE_JPEG;
	else if (strstr(ptr, ".BMP")||strstr(ptr,".ico"))
		return CTYPE_BMP;
	else if (strstr(ptr, ".GIF"))
		return CTYPE_GIF;
	else if (strstr(ptr, ".SWF"))
		return CTYPE_SWF;
	else if (strstr(ptr, ".PJPEG"))
		return CTYPE_PJPEG;
	else if (strstr(ptr, ".CSS"))
		return CTYPE_CSS;
	else if (strstr(ptr, ".JS"))
		return CTYPE_JS;
	else if (strstr(ptr, ".MP4"))
		return CTYPE_MP4;
	else if (strstr(ptr, ".PHP"))
		return CTYPE_HTML;
	else if(strstr(ptr,".HTML") || strstr(ptr,"HTM"))
		return CTYPE_HTML;
	else if(strstr(ptr,".TXT"))
	    return CTYPE_PLAIN;	
	else if(strstr(ptr,".XML"))
	    return CTYPE_XML;	
	else if(strstr(ptr,".SOAPXML"))
	    return CTYPE_SOAPXML;	
	else return CTYPE_OCTEC;
}


